﻿using System;
using System.Web.UI;
using Project.Model;

namespace Web
{
	/// <summary>
	/// 功能：编辑页面
	///       (Plantable)
	/// 作者：luky
	/// 日期：2010-12-28
	/// </summary>
    public partial class DPlantable_Edit : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //此处添加需要绑定的控件

                if (this.SID > 0)
                {
                    // 编辑状态
                    DPlantable model = ManagerFactory.DPlantableManager.Get(this.SID);
                    if (model == null)
                    {
                        Alert("记录不存在!");
                        ExecJsAjax("window.close();", Page);
                        return;
                    }

                    // 绑定页面控件与对象的值
                    base.BindControlsValue(Master.FindControl("ContentPlaceHolder1").Controls, model, true);
                    
                    // 以下添加页面控件自定义显示代码
                    
                }
                else
                {
                    // 新建
                }

                // 查看
                if (Request.Params["isenable"] != null)
                {
                    ClientScript.RegisterStartupScript(typeof(Page), "a", "<script>ChangeToLabel();</script>");
                }

            }

        }

        protected void saveButton_Click(object sender, System.EventArgs e)
        {
            if (IsValid)
            {
                DPlantable model = new DPlantable();
                model.Jlyear = DateTime.Now.Year.ToString();
                //model.Jlyear = "2012";
                try
                {
                    if (this.SID > 0)
                    {
                        // 更新
                        model = ManagerFactory.DPlantableManager.Get(this.SID);
                        
                    }
                    else
                    {
                        // 新建
                    }

                    if (model == null)
                    {
                        Alert("记录不存在!");
                        ExecJsAjax("window.close();", Page);
                        return;
                    }

                    // 绑定页面控件与对象的值
                    model = base.BindControlsValue(Master.FindControl("ContentPlaceHolder1").Controls, model, false);

                    if (this.SID > 0)
                    {
                        // 更新
                        ManagerFactory.DPlantableManager.Update(model);
                    }
                    else
                    {
                        // 新建
                        model.Jlyear = DateTime.Now.Year.ToString();
                        //model.Jlyear = "2012";
                        ManagerFactory.DPlantableManager.Save(model);
                    }
                    Alert("保存成功!");
                    ExecJsAjax("window.opener.searchRefresh();window.close();", Page);
                }
                catch (Exception ex)
                {
                    Alert(ex.Message);
                }
            }
        }
    }
}